package com.ftn;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * User: Jack Fu
 * Date: 2024/9/4
 * Time: 15:09
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class GameWin extends JFrame {

    // 游戏状态
    // 0-未开始 1-游戏中 2-暂停 3-失败 4-通关

    public static int gameState = 0;

    BgObj bgObj = new BgObj();

    PlaneObj planeObj = new PlaneObj(this);

    Boss1Obj boss1Obj = new Boss1Obj(this);

    Boss2Obj boss2Obj = new Boss2Obj(this);

    BossObj bossObj = new BossObj(this);

    WarringObj warningObj = new WarringObj(this);

    Image offScrImage;

    int count = 1;

    public static int planeIndex = 0;
    public static Boss1Obj boss1ObjFinal = null;
    public static Boss2Obj boss2ObjFinal = null;
    public static BossObj bossObjFinal = null;

    public static int score = 0;

    public void launch(){
        this.setVisible(true);
        this.setSize(600, 800);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("飞机大战");
        // 鼠标事件
        this.addMouseListener(new MouseAdapter() {
            /**
             * {@inheritDoc}
             *
             * @param e
             */
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton()==1 && gameState == 0) {
                    gameState = 1;
                }
            }
        });
        // 键盘事件
        this.addKeyListener(new KeyAdapter() {
            /**
             * Invoked when a key has been pressed.
             *
             * @param e
             */
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 32) {
                    if (gameState == 1) {
                        // 游戏中空格，游戏暂停
                        gameState = 2;
                    }else if (gameState == 2) {
                        // 暂停中空格，游戏继续
                        gameState = 1;
                    }
                }
            }
        });

        GameUtils.gameObjs.add(bgObj);
        GameUtils.gameObjs.add(planeObj);
        //GameUtils.gameObjs.add(shellObj);
        planeIndex = GameUtils.gameObjs.indexOf(planeObj);

        while (true) {
            createObj();
            repaint();

            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g1) {
        // 初始化缓存图片
        if (offScrImage == null) {
            offScrImage = this.createImage(600, 800);
        }
        Graphics g = offScrImage.getGraphics();
        g.fillRect(0, 0, 600, 800);

        if (gameState == 0) {
            g.drawImage(GameUtils.bgImage, 0, 0, null);
            g.drawImage(GameUtils.bossImage, 175, 40, null);
            g.drawImage(GameUtils.baozhaImage, 260, 330, null);
            g.drawImage(GameUtils.planeImage, 235, 400, null);

            g.setColor(Color.RED);
            g.setFont(new Font("仿宋", Font.BOLD, 40));
            g.drawString("左键开始游戏", 180, 300);
        }
        if (gameState == 1) {
            /*bgObj.paintSelf(g);
            planeObj.paintSelf(g);
            shellObj.paintSelf(g);*/
            // 添加爆炸元素
            GameUtils.gameObjs.addAll(GameUtils.explodeObjs);

            for (int i = 0; i < GameUtils.gameObjs.size(); i++) {
                GameObj gameObj = GameUtils.gameObjs.get(i);
                gameObj.paintSelf(g);
            }

            // 删除已消失的元素,有些元素需要先加载出来再删除，比如爆炸效果
            GameUtils.gameObjs.removeAll(GameUtils.removeObjs);
            // 清空消失的元素
            //GameUtils.removeObjs.clear();
            // 游戏正式开始时开始计算时间间隔
            count++;
        }
        if (gameState == 2) {
            g.drawImage(GameUtils.bgImage, 0, 0, null);
            GameUtils.drawWord(g, "游戏暂停", 250, 400, 40, Color.RED);
        }
        if (gameState == 3) {
            g.drawImage(GameUtils.bgImage, 0, 0, null);
            GameUtils.drawWord(g, "游戏失败", 250, 400, 40, Color.RED);
        }
        if (gameState == 4) {
            g.drawImage(GameUtils.bgImage, 0, 0, null);
            GameUtils.drawWord(g, "游戏通过，good", 250, 400, 40, Color.RED);
        }

        GameUtils.drawWord(g, score + "分", 40, 70, 30, Color.green);
        g1.drawImage(offScrImage, 0, 0, null);
    }

    public void createObj() {
        if (count % 10 == 0) {
            if (planeObj.times == 1) {
                // 添加子弹
                GameUtils.shellObjs.add(new ShellObj(planeObj.getX() + 50, planeObj.getY() - 80, 25, 97, GameUtils.zidan1Image, 30, this));
            } else if (planeObj.times == 2) {
                GameUtils.shellObjs.add(new ShellObj(planeObj.getX() + 50, planeObj.getY() - 80, 29, 71, GameUtils.zidan2Image, 30, this));
            } else if (planeObj.times == 3) {
                GameUtils.shellObjs.add(new ShellObj(planeObj.getX(), planeObj.getY() - 80, 150, 151, GameUtils.zidan3Image, 30, this));
            }
            GameUtils.gameObjs.add(GameUtils.shellObjs.get(GameUtils.shellObjs.size() - 1)); // 添加最后一个子弹到元素 列表
        }

        // 添加敌机
        if (count % 15 == 0) {
            GameUtils.diji1Objs.add(new Enemy1Obj((int) (Math.random() * 90) * 6, 0, 55, 38, GameUtils.diji1Image, 5, this));
            //GameUtils.diji1Objs.add(new Enemy1Obj((int) (Math.random() * 95 * 6), 0, 53, 38, GameUtils.diji1Image, 5, this));
            GameUtils.gameObjs.add(GameUtils.diji1Objs.get(GameUtils.diji1Objs.size() - 1)); // 添加最后一个子弹到元素 列表
        }

        if (count % 150 == 0) {
            if (count % 200 == 0) {
                GameUtils.diji2Objs.add(new Enemy2Obj((int) (Math.random() * 133) * 3, 0, 100, 73, GameUtils.diji2Image, 2, this));
                //GameUtils.diji1Objs.add(new Enemy1Obj((int) (Math.random() * 95 * 6), 0, 53, 38, GameUtils.diji1Image, 5, this));
                GameUtils.gameObjs.add(GameUtils.diji2Objs.get(GameUtils.diji2Objs.size() - 1)); // 添加最后一个子弹到元素 列表
            }
            // 每50次 添加一个敌机子弹
            if (GameUtils.diji2Objs.size() > 0) {
                int x = GameUtils.diji2Objs.get(GameUtils.diji2Objs.size() - 1).getX();
                int y = GameUtils.diji2Objs.get(GameUtils.diji2Objs.size() - 1).getY();
                GameUtils.dijizidanObjs.add(new Enemy2BulletObj(x + 95, y + 138, 20, 50, GameUtils.dijizidanImage, 2, this));
                GameUtils.gameObjs.add(GameUtils.dijizidanObjs.get(GameUtils.dijizidanObjs.size() - 1));
            }
        }

        // 添加boss
        if (count % 200 == 0) {
            if (!GameUtils.gameObjs.contains(boss1Obj) && !boss1Obj.gg) {
                GameUtils.gameObjs.add(boss1Obj);
                boss1ObjFinal = boss1Obj;
            }
            if (count % 400 == 0) {
                if (!GameUtils.gameObjs.contains(boss2Obj)) {
                    GameUtils.gameObjs.add(boss2Obj);
                    boss2ObjFinal = boss2Obj;
                }
            }
        }

        // 添加boss子弹
        if (count % 40 == 0) {
            if (GameUtils.gameObjs.contains(boss1Obj)) {
                GameUtils.boss1BulletObjs.add(new Boss1BulletObj(boss1Obj.getX() + 115, boss1Obj.getY() + 210, 20, 50, GameUtils.boss1zidanImage, 1, this));
                GameUtils.gameObjs.add(GameUtils.boss1BulletObjs.get(GameUtils.boss1BulletObjs.size() - 1));
            }
        }

        if (count % 80 == 0) {
            if (GameUtils.gameObjs.contains(boss2Obj)) {
                GameUtils.boss2BulletObjs.add(new Boss2BulletObj(boss2Obj.getX() + 70, boss2Obj.getY() + 210, 20, 50, GameUtils.boss2zidanImage, 5, this));
                GameUtils.gameObjs.add(GameUtils.boss2BulletObjs.get(GameUtils.boss2BulletObjs.size() - 1));
            }
        }

        // 添加大boss
        if (count == 130) {
            if (!GameUtils.gameObjs.contains(bossObj)) {
                bossObjFinal = bossObj;
                GameUtils.gameObjs.add(bossObj);
            }
        }

        if (count % 20 == 0) {
            if (GameUtils.gameObjs.contains(bossObj)) {
                GameUtils.bossBulletObjs.add(new BossBulletObj(bossObj.getX() + 64, bossObj.getY() + 210, 20, 50, GameUtils.bosszidanImage, 2, this));
                GameUtils.gameObjs.add(GameUtils.bossBulletObjs.get(GameUtils.bossBulletObjs.size() - 1));

                if (count % 40 == 0) {
                    GameUtils.bossBulletObjs.add(new BossBulletObj(bossObj.getX() + 64, bossObj.getY() + 210, 20, 50, GameUtils.boss1zidanImage, 4, this));
                    GameUtils.gameObjs.add(GameUtils.bossBulletObjs.get(GameUtils.bossBulletObjs.size() - 1));
                }

                if (count % 60 == 0) {
                    GameUtils.bossBulletObjs.add(new BossBulletObj(bossObj.getX() + 64, bossObj.getY() + 210, 20, 50, GameUtils.boss2zidanImage, 6, this));
                    GameUtils.gameObjs.add(GameUtils.bossBulletObjs.get(GameUtils.bossBulletObjs.size() - 1));
                }
            }
        }

        // 添加警告
        if (count == 1250) {
            if (!GameUtils.gameObjs.contains(warningObj)) {
                GameUtils.gameObjs.add(warningObj);
            }
        }

        // 删除警告，boss来了
        if (count == 1280) {
            GameUtils.removeObjs.add(warningObj);
        }
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
